<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/2/11 17:41
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */

class Setting_Model extends CI_Model
{
    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $use
     * @param $key
     */
    public function getSetting($use, $key)
    {
        $db = clone($this->db);
        $where = [
            'use' => $use,
            'key' => $key
        ];

        $q = $db->get_where('setting', $where);
        $row = $q->row_array();
        if ($row) {
            $result = json_decode($row['value'], true);
            return $result;
        }
        return false;
    }

    /**
     * @param $use
     * @param $key
     * @return bool|mixed
     */
    public function setSetting($use, $key, $data)
    {
        $db = clone($this->db);
        $where = [
            'use' => $use,
            'key' => $key
        ];

        $q = $db->get_where('setting', $where);
        $row = $q->row_array();
        if ($row) {
            $db->where($where);
            $db->update('setting', ['value' => json_encode($data)]);
            return $db->affected_rows();
        } else {
            $db->insert('setting', [
                'use' => $use,
                'key' => $key,
                'value' => json_encode($data)
            ]);
            return $db->insert_id();
        }
    }
}
